<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>代理模式与延迟加载：优化性能的设计艺术</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            bottom: -50%;
            left: -50%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 1px, transparent 1px);
            background-size: 50px 50px;
            animation: drift 20s linear infinite;
        }
        
        @keyframes drift {
            from { transform: translate(0, 0); }
            to { transform: translate(50px, 50px); }
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            padding: 1.5rem;
            overflow-x: auto;
            position: relative;
            font-family: 'Consolas', 'Monaco', monospace;
        }
        
        .code-block::before {
            content: '';
            position: absolute;
            top: 10px;
            left: 15px;
            width: 12px;
            height: 12px;
            background: #ff5f56;
            border-radius: 50%;
            box-shadow: 20px 0 0 #ffbd2e, 40px 0 0 #27c93f;
        }
        
        .section-title {
            position: relative;
            display: inline-block;
            margin-bottom: 2rem;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea, #764ba2);
            border-radius: 2px;
        }
        
        .feature-card {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 2rem;
            border-radius: 16px;
            position: relative;
            overflow: hidden;
            transition: all 0.3s ease;
        }
        
        .feature-card::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.2) 0%, transparent 70%);
            transform: rotate(45deg);
            transition: all 0.5s ease;
        }
        
        .feature-card:hover::before {
            transform: rotate(45deg) translate(20px, 20px);
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #e0c3fc 0%, #8ec5fc 100%);
            padding: 1.5rem;
            border-radius: 12px;
            border-left: 4px solid #667eea;
            margin: 2rem 0;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 relative">
        <div class="container mx-auto px-6 relative z-10">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    代理模式与延迟加载
                </h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">
                    优化性能的设计艺术
                </p>
                <div class="flex justify-center space-x-4">
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full">
                        <i class="fas fa-layer-group mr-2"></i>
                        结构型模式
                    </span>
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full">
                        <i class="fas fa-rocket mr-2"></i>
                        性能优化
                    </span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- Introduction -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">代</span>理模式（Proxy Pattern）是一种结构型设计模式，它通过创建代理对象来控制对真实对象的访问。利用代理模式实现延迟加载（Lazy Loading）是一种有效的优化手段，尤其在处理资源密集型对象或需要节省内存时。
            </p>
        </div>

        <!-- Concept Visualization -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-6">
                <i class="fas fa-lightbulb text-yellow-500 mr-3"></i>
                核心概念关系图
            </h2>
            <div class="mermaid">
                graph TD
                    A[客户端] -->|请求| B[代理对象]
                    B -->|延迟创建| C[真实对象]
                    B -->|控制访问| D[访问控制]
                    B -->|性能优化| E[缓存机制]
                    C -->|资源密集| F[初始化成本]
                    D -->|权限验证| G[安全性]
                    E -->|减少开销| H[性能提升]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Lazy Loading Concept -->
        <div class="grid md:grid-cols-2 gap-8 mb-8">
            <div class="content-card rounded-2xl p-8">
                <h3 class="section-title text-2xl font-bold text-gray-800 mb-4">
                    <i class="fas fa-clock text-blue-500 mr-2"></i>
                    延迟加载的概念
                </h3>
                <p class="text-gray-700 leading-relaxed">
                    延迟加载是一种设计模式，旨在推迟对象的实例化，直到实际需要该对象时再创建。通过这种方式，可以避免不必要的资源消耗，优化系统性能。
                </p>
                <div class="highlight-box mt-4">
                    <p class="text-sm font-medium">
                        <i class="fas fa-info-circle mr-2"></i>
                        核心思想：按需加载，避免浪费
                    </p>
                </div>
            </div>
            
            <div class="feature-card">
                <h4 class="text-2xl font-bold mb-4">
                    <i class="fas fa-chart-line mr-2"></i>
                    性能优势
                </h4>
                <ul class="space-y-3">
                    <li class="flex items-start">
                        <i class="fas fa-check-circle mt-1 mr-3"></i>
                        <span>减少初始化时间</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle mt-1 mr-3"></i>
                        <span>节省内存资源</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle mt-1 mr-3"></i>
                        <span>提升应用响应速度</span>
                    </li>
                </ul>
            </div>
        </div>

        <!-- Implementation -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-6">
                <i class="fas fa-code text-green-500 mr-3"></i>
                实现方式
            </h2>

            <!-- Step 1 -->
            <div class="mb-8">
                <h3 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
                    <span class="bg-gradient-to-r from-purple-500 to-pink-500 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3">1</span>
                    创建真实对象接口和实现
                </h3>
                <p class="text-gray-700 mb-4">
                    首先，定义一个接口，描述真实对象的行为，以及一个具体类实现该接口。
                </p>
                <div class="code-block">
                    <pre><code>public interface Subject {
    void request();
}

public class RealSubject implements Subject {
    public RealSubject() {
        // 资源密集型的初始化
        System.out.println("RealSubject initialized.");
    }

    @Override
    public void request() {
        System.out.println("Request processed.");
    }
}</code></pre>
                </div>
            </div>

            <!-- Step 2 -->
            <div class="mb-8">
                <h3 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
                    <span class="bg-gradient-to-r from-purple-500 to-pink-500 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3">2</span>
                    创建代理类
                </h3>
                <p class="text-gray-700 mb-4">
                    代理类实现相同的接口，并持有真实对象的引用。代理类在调用真实对象的方法之前，检查是否已经初始化真实对象。如果未初始化，则创建它。
                </p>
                <div class="code-block">
                    <pre><code>public class ProxySubject implements Subject {
    private RealSubject realSubject;

    @Override
    public void request() {
        // 延迟加载逻辑
        if (realSubject == null) {
            realSubject = new RealSubject();
        }
        realSubject.request();
    }
}</code></pre>
                </div>
            </div>

            <!-- Step 3 -->
            <div class="mb-8">
                <h3 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
                    <span class="bg-gradient-to-r from-purple-500 to-pink-500 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3">3</span>
                    使用代理
                </h3>
                <p class="text-gray-700 mb-4">
                    客户端通过代理类与真实对象进行交互。客户端调用<code class="bg-gray-200 px-2 py-1 rounded">request</code>方法时，代理会决定是否实例化真实对象。
                </p>
                <div class="code-block">
                    <pre><code>public class Client {
    public static void main(String[] args) {
        Subject subject = new ProxySubject();
        subject.request();  // 第一次调用时初始化真实对象
        subject.request();  // 后续调用直接使用已初始化的对象
    }
}</code></pre>
                </div>
            </div>
        </div>

        <!-- Performance Impact -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-6">
                <i class="fas fa-tachometer-alt text-red-500 mr-3"></i>
                潜在的性能影响
            </h2>

            <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-6">
                <!-- Positive Impacts -->
                <div class="bg-green-50 border-l-4 border-green-500 p-6 rounded-lg">
                    <h4 class="font-bold text-green-800 mb-3 